package com.ziyia.a8_3_1;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import java.util.StringJoiner;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.init)
    Button init;

    @BindView(R.id.insert)
    Button insert;

    @BindView(R.id.select)
    Button select;
    MySQLiteHandler mySQLiteHandler = new MySQLiteHandler(this, "users", null, 2);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


        init.setOnClickListener(v -> {
            mySQLiteHandler.getWritableDatabase();
            String databaseName = mySQLiteHandler.getDatabaseName();
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle(getLocalClassName())
                    .setMessage(databaseName)
                    .create()
                    .show();
        });


        insert.setOnClickListener(v -> {
            SQLiteDatabase database = mySQLiteHandler.getWritableDatabase();

            ContentValues contentValues = new ContentValues();
            contentValues.put("username", "root");
            contentValues.put("password", "123456");

            database.insert("user", null, contentValues);
        });


        select.setOnClickListener(v -> {
            SQLiteDatabase database = mySQLiteHandler.getReadableDatabase();
            Cursor users = database.query("user", null, null, null, null, null, null);
            StringBuffer buffer = new StringBuffer();

            if (users.moveToFirst()) {
                do {
                    StringJoiner stringJoiner = new StringJoiner("        |        ");
                    int id = users.getInt(users.getColumnIndex("id"));
                    String username = users.getString(users.getColumnIndex("username"));
                    String password = users.getString(users.getColumnIndex("password"));
                    stringJoiner.add(id+"");
                    stringJoiner.add(username);
                    stringJoiner.add(password);
                    buffer.append(stringJoiner);
                    buffer.append('\n');
                } while (users.moveToNext());
            }
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("查询数据")
                    .setMessage(buffer)
                    .create()
                    .show();
        });

    }
}


class MySQLiteHandler extends SQLiteOpenHelper {
    private Context context;

    private String sql1 = "create table user(id integer primary key autoincrement, username text, password text);";
    private String sql2 = "create table admin(id integer primary key autoincrement, uid ingeger);";

    public MySQLiteHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql1);
        db.execSQL(sql2);
        Toast.makeText(context, "数据库初始化成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion == 1) {
            db.execSQL(sql1);
        }

        if (newVersion == 2) {
            db.execSQL(sql2);
        }
    }
}